package com.example.demo.util;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClient431Util {

    private static final RequestConfig config;
    
    public static final String DEFAULT_SEND_CHARSET = "UTF-8";
    
    public static final String DEFAULT_RES_CHARSET = "UTF-8";
 
    static {
        config = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(60000).build();
    }

    
    public static String doPost(Map<String, String> params,String url) throws Exception{
        return doPost(params,url,DEFAULT_SEND_CHARSET,DEFAULT_RES_CHARSET);
    }

    
    
    /**
     * HTTP Post 获取内容
     * @param params 请求的参数
     * @param url  请求的url地址 ?之前的地址
     * @param reqCharset    编码格式
 	* @param resCharset    编码格式
     * @return    页面内容
     * @throws Exception 
     */
    public static String doPost(Map<String,String> params,String url,String reqCharset,String resCharset) throws Exception{
    	CloseableHttpClient httpClient = getSingleSSLConnection();
    	CloseableHttpResponse response = null;
        if(isBlank(url)){
            return null;
        }
        try {
//            System.out.println("发送的网址："+ url);//要发送数据
//            System.out.println("发送的数据："+ params);//要发送数据

            List<NameValuePair> pairs = null;
            if(params != null && !params.isEmpty()){
                pairs = new ArrayList<NameValuePair>(params.size());
                for(Map.Entry<String,String> entry : params.entrySet()){
                    String value = entry.getValue();
                    if(value != null){
                        pairs.add(new BasicNameValuePair(entry.getKey(),value));
                    }
                }
            }
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
            httpPost.addHeader("Connection" , "close");
            if(pairs != null && pairs.size() > 0){
                httpPost.setEntity(new UrlEncodedFormEntity(pairs,reqCharset==null?DEFAULT_SEND_CHARSET:reqCharset));
            }
            response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
         	   Header header = response.getFirstHeader("location");
         	   return header.getValue();
            }
            HttpEntity entity = response.getEntity();
            String result = null;
            if (entity != null){
                result = EntityUtils.toString(entity, resCharset==null?DEFAULT_RES_CHARSET:resCharset);
            }
            EntityUtils.consume(entity);
            response.close();
            return result;
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new Exception (e);
        } finally{
         	if(response!=null)
             try {
 				response.close();
 			} catch (IOException e) {
 				throw new Exception (e);
 			}
         }
    }

    
	 /**
	  * 创建单向ssl的连接
	  * @return
	 * @throws Exception 
	  */
	private static CloseableHttpClient getSingleSSLConnection() throws Exception{
	 	CloseableHttpClient httpClient = null;
	 	try {
				SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null,new TrustStrategy() {
					@Override
					public boolean isTrusted(X509Certificate[] paramArrayOfX509Certificate,
							String paramString) throws CertificateException {
						return true;
					}
				}).build();
				SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
				httpClient =  HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(config).build();
				return httpClient;
			} catch (Exception e) {
			     e.printStackTrace();
			     throw new Exception(e);
			}
	}
    
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }
}
